<template>
  <pageContainer class="contentRole">
    <template slot="header">
      <div class="title">
        <i class="line"></i>
        <p class="text">查询条件</p>
      </div>
      <div class="search">
        <el-form :inline="true" :model="queryParams" class="demo-form-inline">
          <el-form-item label="用户名称：">
            <el-input size="small" v-model="queryParams.userName" placeholder="请输入用户名称"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="用户状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <template slot="center">
      <div class="title">
        <i class="line"></i>
        <p class="text">用户列表</p>
      </div>
      <div class="table" ref="table">
        <el-table
          :data="tableData"
          :max-height="maxHeight"
          :header-cell-style="{textAlign: 'center',color: '#fff',backgroundColor: '#223450'}"

          style="width: 100%">
          <el-table-column
            type="index"
            align="center"
            width="100"
            label="序号">
          </el-table-column>

          <el-table-column label="用户名称" align="center" key="userName" prop="userName"  :show-overflow-tooltip="true" />
          <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName"  :show-overflow-tooltip="true" />
          <el-table-column label="行政区划" align="center" key="areaName" prop="area.areaName" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber"  width="120" />
          <el-table-column label="状态" align="center" key="status" >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                disabled
                inactive-value="1"
                v-if="scope.row.userId !== 1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime"  width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>

          <el-table-column
          align="center"
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" v-if="scope.row.userId !== 1" @click="handleUpdate(scope.row)" style="padding:0" v-hasPermi="['system:user:resetPwd']">重置密码</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </template>
    <template slot="bottom">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

       <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.roleVisible" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input disabled v-model="form.nickName" placeholder="请输入用户昵称" maxlength="30" />
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="行政区划" prop="adcode">
                <el-cascader
                disabled
                 v-model="form.adcode"
                :options="areaOptions"
                :props="{ checkStrictly: true,label:'label' ,value:'id'}"
                clearable></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input disabled v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input disabled v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item  label="用户名称" prop="userName">
              <el-input disabled v-model="form.userName" placeholder="请输入用户名称" maxlength="30" />
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="用户密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码" type="password" maxlength="20" show-password/>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </template>
  </pageContainer>
</template>

<script>

import pageContainer from '../../../components/pageContainer.vue'
import { listUser, changeUserStatus, areaTreeSelect, getUser, updateUser, addUser, delUser, resetPwd } from '@/api/user'
import { dict } from '@/utils/dict'

export default {
  components: { pageContainer },
  data() {
    return {
      dict: dict,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      tableData: [],
      maxHeight: null,
      // 表单参数
      form: {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: '0',
        remark: undefined,
        postIds: [],
        roleIds: []
      },
      rules: {
        userName: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '用户昵称不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '用户密码不能为空', trigger: 'blur' },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        email: [
          {
            type: 'email',
            message: "'请输入正确的邮箱地址",
            trigger: ['blur', 'change']
          }
        ],
        phonenumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]
      },
      ids: [],
      areaOptions: [],
      roleOptions: [],
      dialog: {
        roleVisible: false,
        title: ''
      }
    }
  },
  mounted() {
    // this.getMaxTableHeight()
    this.getList()
    this.getAreaTreeselect()
  },
  watch: {
    maxHeight: {
      handler(newV) {
        this.maxHeight = newV
      },
      deep: true
    }
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置密码操作 */
    handleUpdate(row) {
      const data = {
        ...row
      }
      resetPwd(data).then(response => {
        if (response.code === 200) {
          this.$message.success(response.msg)
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids
      this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds)
      }).then(() => {
        this.getList()
      }).catch(() => {})
    },
    // 取消按钮
    cancel() {
      this.dialog.roleVisible = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: '0',
        remark: undefined,
        postIds: [],
        roleIds: []
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.userId !== undefined) {
            updateUser(this.form).then(response => {
              this.dialog.roleVisible = false
              this.getList()
            })
          } else {
            this.form.adcode = this.form.adcode[this.form.adcode.length - 1]
            addUser(this.form).then(response => {
              this.dialog.roleVisible = false
              this.getList()
            })
          }
        }
      })
    },
    getAreaTreeselect() {
      areaTreeSelect().then(response => {
        this.areaOptions = response.data
      })
    },
    /** 查询用户列表 */
    getList() {
      listUser(this.queryParams).then(response => {
        this.tableData = response.rows
        this.total = response.total
      }
      )
    },
    // 用户状态修改
    handleStatusChange(row) {
      const text = row.status === '0' ? '启用' : '停用'
      this.$confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function() {
        return changeUserStatus(row.userId, row.status)
      }).then(() => {
        this.$message({
          message: text + '成功',
          type: 'success'
        })
      }).catch(function() {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    // 新增
    add() {
      this.reset()
      this.getAreaTreeselect()
      getUser().then(response => {
        this.postOptions = response.data.posts
        this.roleOptions = response.data.roles

        this.dialog.roleVisible = true
        this.dialog.title = '新增'
        // this.form.password = this.initPassword
      })
    },
    handleClose() {
      this.dialog.roleVisible = false
    },
    getMaxTableHeight() {
      const windowInnerHeight = this.$refs.table
      this.maxHeight = windowInnerHeight.offsetHeight
    },

    handleSizeChange(val) {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = val
      console.log(`每页 ${val} 条`)
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      console.log(`当前页: ${val}`)
      this.getList()
    },
    // 修改
    edit(row) {
      this.dialog.roleVisible = true
      this.dialog.title = '修改'
    }
  }
}
</script>

<style lang="scss" scoped>
.contentRole{
  .title{
    display: flex;
    align-items: center;
    height: 30px;
    .line{
      width: 5px;
      height: 25px;
      background: #409EFF;
      margin-right: 5px;
    }
    .text{
      color: #fff;
    }
  }
  .search{
    height: calc(100% - 30px);
    display: flex;
    align-items: center;
    .demo-form-inline{
      padding-left: 10px;
      :deep(.el-form-item){
        margin-bottom: 0;
      }
    }
  }
  .table{
    height: calc(100% - 40px);
    margin-top: 10px;
  }
}
/*最外层透明*/
:deep(.el-table), :deep(.el-table__expanded-cell){
  background-color: transparent;
}
/* 表格内背景颜色 */
:deep(.el-table th),
:deep(.el-table tr),
:deep(.el-table td) {
  background-color: transparent;
  color: #C9E9FF;
  height: 30px;
}
:deep(.el-table thead) {
  color: #7EC1FF;
  font-size: 14px;
}
:deep(.el-table--enable-row-hover){
   background-color: transparent;
}
:deep(.el-table .warning-row) {
 background: rgba(51, 91, 127, 0.6);
}
:deep(.el-table){
  --el-table-border-color: rgba(51, 91, 127, 0.6) !important;

}
:deep(.el-table tbody tr:hover > td) {
            background: rgba(51, 91, 127, 0.4) !important;
        }
   :deep(.el-input__inner){
  background: rgba(51, 91, 127, 1);
  box-shadow:none;
  border:none;
  color: #fff;

}
:deep(.el-pager li){
  background: rgba(51, 91, 127, 1);
  box-shadow:none;
  border:none;
  color: #fff;
}
:deep(.btn-prev){
  background: rgba(51, 91, 127, 1);
  box-shadow:none;
  border:none;
  color: #fff;
}
:deep(.el-pagination button:disabled){
  background: rgba(51, 91, 127, 1);
  box-shadow:none;
  border:none;
  color: #fff;

}
:deep(.el-pagination__total),:deep(.el-pagination__jump){
  color: #fff;
}
:deep(.el-table td.el-table__cell),:deep(.el-table th.el-table__cell.is-leaf){
  border-color: rgba(231,234,241,0.2);
}
:deep(.el-table::before){
  background: rgba(231,234,241,0.2);
}
:deep(.el-form-item__label){
  color: #A3B3BE;
}
:deep(.el-dialog){
  background: rgba(0,28,55,0.85);
}
:deep(.el-dialog__header){
  border-bottom: 1px solid rgba(120, 189, 255, 1);
  margin-right: 0px;
  margin-bottom:10px
}
:deep(.el-dialog__title){
  color: #41B0FF;
  font-size: 16px;
}

:deep(.el-dialog__headerbtn .el-dialog__close){
  color: rgba(70, 174, 255, 1);
}
</style>
